package com.rankeiot.platform.controller;

import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.rankeiot.core.EasyCorePlugin;
import com.rankeiot.core.config.ConfigManager;
import com.rankeiot.core.data.response.Resp;
import com.rankeiot.core.event.GetJsConfigEvent;
import com.rankeiot.core.tags.JsonTag;
import com.rankeiot.core.util.JSONUtil;
import com.rankeiot.easy.vue.EasyVueConfiguration;
import com.rankeiot.platform.inner.view.JFinalViewResolver;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
public class IndexController {
    final ApplicationContext context;
    private Engine indedxEngine;
    @Value("${spring.profiles.active:default}")
    private String active;
    @GetMapping(value = "/index.html",produces = "text/html;charset=UTF-8")
    public ResponseEntity index(@RequestParam(required = false) String debug){

        if(!EasyCorePlugin.hasDataSource){
           // return "location.href='install.html'";
           // mv.setViewName("redirect:install.html");
            return ResponseEntity.status(HttpStatus.SEE_OTHER)
                    .header("Location", "install.html")
                    .build();
           // return mv;
        }
        boolean isDebug=debug!=null;
        Map model = new HashMap();
        Map frontConfig=ConfigManager.getFrontConfigs();
        model.put("configs",frontConfig);
        model.put("isDev", EasyVueConfiguration.devModel||isDebug);
        GetJsConfigEvent getJsConfigEvent=new GetJsConfigEvent();
        // ExtendsManager.invoke(point);
        context.publishEvent(getJsConfigEvent);

        model.put("extend", getJsConfigEvent.getConfigJs());
       // mv.getModelMap().
       // mv.setViewName("/index");
       Template template = getTemplateEngine().getTemplate("index.html");
       return ResponseEntity.ok(template.renderToString(model));
    }
    private synchronized Engine getTemplateEngine(){
        if(indedxEngine==null){
            indedxEngine=new Engine();
            indedxEngine.setBaseTemplatePath("/static/");
            indedxEngine.addDirective("json", JsonTag.class);
            indedxEngine.setToClassPathSourceFactory();
            if("dev".equals(active)){
                indedxEngine.setDevMode(true);
            }
        }
        return indedxEngine;
    }
}
